package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.bean.UserPostListDateBan;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间 2017 年 09 月 10 日
 * 作者  Z2043 welbeing to hangzou com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 */
public class MyPostingOnCircleAdapte extends  RecyclerView.Adapter<MyPostingOnCircleAdapte.ViewHolder> implements View.OnClickListener{
    private static final String TAG ="MyPostingOnCircleAdapte" ;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private List<UserPostListDateBan.DataBean> mlist = new ArrayList<>();
    private  Context mcontext;
    private String user_cus_logo;
    private  String user_name;

    public MyPostingOnCircleAdapte(Context context) {
        mcontext = context;

    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public MyPostingOnCircleAdapte.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_posting_on_circle_recyclerview,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyPostingOnCircleAdapte.ViewHolder holder, int position) {
        Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+user_cus_logo).transform(new GlideCircleTransform(mcontext)).into(holder.iv_personal_head);
        holder.tv_name.setText(user_name);
        holder.tv_date.setText(mlist.get(position).addtime);
        holder.tv_title.setText(mlist.get(position).po_name);
        holder.tv_content.setText(mlist.get(position).content);
        holder.tv_thumb_up.setText(mlist.get(position).state+"");
        holder.tv_commentaries.setText(mlist.get(position).count);
        holder.tv_circle_classification.setText(mlist.get(position).cat_name);
        String po_url =  mlist.get(position).po_url;
        if(po_url == null || po_url.equals("") ){
            holder.ll_01.setVisibility(View.GONE);
        }else{
            String[] arr = po_url.split(",");
            if(arr.length == 1){
                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[0]).into(holder.iv_published_pictures_01);
                holder.iv_published_pictures_02.setVisibility(View.GONE);
                holder.iv_published_pictures_03.setVisibility(View.GONE);
            }else  if(arr.length == 2){
                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[0]).into(holder.iv_published_pictures_01);
                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[1]).into(holder.iv_published_pictures_02);
                holder.iv_published_pictures_03.setVisibility(View.INVISIBLE);
            }else {
                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[0]).into(holder.iv_published_pictures_01);
                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[1]).into(holder.iv_published_pictures_02);
                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[2]).into(holder.iv_published_pictures_03);
            }

        }


    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }
    public void addItem(List<UserPostListDateBan.DataBean> bean, String cus_logo, String cus_name) {
        mlist.addAll(bean);
        user_cus_logo = cus_logo;
        user_name = cus_name;
        notifyDataSetChanged();
    }
    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(view);
        }
    }

    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_name;
        private  TextView tv_date;
        private  TextView tv_title;
        private  TextView tv_content;
        private  TextView tv_thumb_up;
        private  TextView tv_commentaries;
        private  TextView tv_circle_classification;
        private ImageView iv_personal_head;
        private ImageView iv_published_pictures_01;
        private ImageView iv_published_pictures_02;
        private ImageView iv_published_pictures_03;
        private LinearLayout ll_01;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_name =  (TextView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_tv_name);
            tv_date =  (TextView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_tv_date);
            tv_title =  (TextView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_tv_title);
            tv_content =  (TextView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_tv_content);
            tv_thumb_up =  (TextView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_tv_thumb_up);
            tv_circle_classification =  (TextView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_tv_circle_classification);
            tv_commentaries =  (TextView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_tv_commentaries);
            iv_personal_head =  (ImageView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_iv_personal_head);
            iv_published_pictures_01 =  (ImageView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_iv_published_pictures_01);
            iv_published_pictures_02 =  (ImageView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_iv_published_pictures_02);
            iv_published_pictures_03 =  (ImageView) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_iv_published_pictures_03);
            ll_01 =  (LinearLayout) itemView.findViewById(R.id.item_my_posting_on_circle_recyclerview_ll_01);
        }
    }
}
